/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vienthonga.homepage.action;

import com.vienthonga.hibernate.pojo.TProduct;
import com.vienthonga.homepage.logic.ShowMobilephoneLogic;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author DoanHa
 */
public class ShowMobilephoneAction extends Action {

    private static final Logger logger = Logger.getLogger(
            ShowMobilephoneAction.class.getName());
    private static final String SUCCESS = "success";

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.log(Level.INFO, mapping.getPath());

        int pageIndex = 1;
        if (request.getParameter("page") != null) {
            pageIndex = Integer.parseInt(request.getParameter("page"));
        }

        //kiem tra action parameter : new : show ra cac dien thoai moi
        //lay dien thoai moi va thuc hien phan trang
        String action = "";
        if (request.getParameter("action") != null) {
            action = request.getParameter("action");
        }

        List<TProduct> newMobilephones = null;
        ShowMobilephoneLogic logic = new ShowMobilephoneLogic();

        //Moi action tuong ung se co 1 hanh dong cu the
        if (action.equals("new")) {
            newMobilephones = logic.getNewMobilephone(pageIndex);
        } else if (action.equals("normalMobilephone")) {
            newMobilephones = logic.getNormalMobilephone(pageIndex);
        } else if (action.equals("mediumMobilephone")) {
            newMobilephones = logic.getMediumMobilephone(pageIndex);
        } else if (action.equals("highMobilephone")) {
            newMobilephones = logic.getHighMobilephone(pageIndex);
        } else {
            newMobilephones = logic.getSuperMobilephone(pageIndex);
        }

        request.setAttribute("mobilePhones", newMobilephones);
        request.setAttribute("NumberOfPage", logic.caculatePaging(action));
        request.setAttribute("Action", action);


        return mapping.findForward(SUCCESS);
    }
}
